package com.shop.manager.controller;

//import com.shop.manager.entity.OrderBean;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.Order;
import com.base.common.entity.OrderItem;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.mapper.OrderItemMapper;
import com.shop.manager.service.OrderItemService;
import com.shop.manager.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 订单增删改查
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderItemService orderItemService;

    /**
     * @Description 注入service对象
     */
    @Autowired
    private OrderService orderService;

    /**
     * @Description 添加订单
     * @param order 订单对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public HttpResult add(@RequestBody(required = false) Order order){
        try {
            orderService.add(order);
            return HttpResult.ok("添加订单成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("添加订单失败!");
    }

    /**
     * @Description 批量删除订单
     * @param ids 订单id的数组
     * @return
     */
    @RequestMapping("/delete")
    public HttpResult delete(@RequestBody Long[] ids){
        try {
            orderService.deleteByIds(ids);
            //删除成功
            return HttpResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //删除异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 修改订单
     * @param order 修改的订单对象
     * @return
     */
    @RequestMapping("/update")
    public HttpResult update(@RequestBody(required = false) Order order){
        try {
            orderService.update(order);
            return HttpResult.ok("修改订单成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("修改订单失败!");
    }

    /**
     * @Description 根据id查询订单
     * @param id
     * @return
     */
    @RequestMapping("/getById")
    public HttpResult getById(@RequestParam("id") Long id){
        return HttpResult.ok(orderService.findOne(id));
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param order 查询条件对象
     * @return
     */
    @RequestMapping("/getAll")
    public HttpResult getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("size") Integer size, @RequestBody(required = false) Order order){
        try {
            //分页查询
            PageResult result = orderService.findPage(pageNum, size, order);
            List<Order> orderList= (List<Order>) result.getRows();
            for (Order order1: orderList) {
                OrderItem orderItem = new OrderItem();
                orderItem.setOrderId(order1.getOrderId());
                List<OrderItem> orderItemList = orderItemService.findByWhere(orderItem);
                order1.setOrderItemList(orderItemList);
            }
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error("查询订单异常");
        }
    }


}
